import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class HiDefend {
  run(Widget app) {
    //框架异常
    FlutterError.onError = (FlutterErrorDetails details) async {
      FlutterError.presentError(details);
      //线上环境，走上报逻辑
      if (kReleaseMode) {
        _reportError(details.exception, details.stack!);
      } else {
        //开发期间，走Console抛出
        FlutterError.dumpErrorToConsole(details);
      }
    };

    PlatformDispatcher.instance.onError = (error, stack) {
      _reportError(error, stack);
      return true;
    };

    runApp(app);
  }

  ///通过接口上报异常
  _reportError(Object error, StackTrace s) {
    debugPrint('catch error:$error');
  }
}
